﻿#include <easyx.h>//图形库
#include <stdio.h>
#include <stdlib.h>//生成随机数
#include <time.h>
#include <math.h>

#define Ai_num 10
#define Food_num 100
#define whire 1200
#define high 500

typedef struct
{
	int x;
	int y;
	float r;
	bool flag;
	int vx;
	int vy;
}qiu;

qiu player;//玩家
qiu ai[Ai_num];//人坤
qiu food[Food_num];//食物


void init(void)
{
	initgraph(whire+20, high+20, EX_SHOWCONSOLE);//初始画布大小
	srand((unsigned int)time(NULL));//种子初始化

	player.x = whire / 2;
	player.y = high / 2;
	player.r = 10;
	player.flag = 1;
	

	for (int i = 0; i < Ai_num; i++)//ai初始化
	{
		ai[i].x = rand() % whire;
		ai[i].y = rand() % high;
		ai[i].r = 10;
		ai[i].flag = 1;
		
	}

	for (int i = 0; i < Food_num; i++)//食物初始化
	{
		if (food[i].flag == 0)
		{
			food[i].x = rand() % whire;
			food[i].y = rand() % high;
			if (Food_num % 2 != 0)
				food[i].r = 3;
			else
				food[i].r = 2;
			food[i].flag = 0;
			
		}
	}
}
void player_move(void)
{
	if (GetKeyState('W') & 0x8000 || GetKeyState('w')&0x8000)
	{
		clearcircle(player.x, player.y, player.r);
		player.y--;
		
	}
	if (GetKeyState('S') & 0x8000 || GetKeyState('s') & 0x8000)
	{	
		clearcircle(player.x, player.y, player.r);
		player.y++;
	}
	if (GetKeyState('A') & 0x8000 || GetKeyState('a') & 0x8000)
	{
		clearcircle(player.x, player.y, player.r);
		player.x--;
	}
	if (GetKeyState('D') & 0x8000 || GetKeyState('d') & 0x8000)
	{
		clearcircle(player.x, player.y, player.r);
		player.x++;
	}
	if (player.x >= whire)
		player.x = whire;
	if (player.y >= high)
		player.y = high;
	if (player.x <= 0)
		player.x = 0;
	if (player.y <= 0)
		player.y = 0;
}

void Ai_move(void)
{
	for (int i = 0; i < Ai_num; i++)
	{
		setfillcolor(GREEN);
		clearcircle(ai[i].x, ai[i].y, ai[i].r);
		if (rand() % 100 < 5)
		{
			ai[i].vx = rand() % 5 - 2;
			ai[i].vy = rand() % 5 - 2;
		}
		ai[i].x += ai[i].vx;
		ai[i].y += ai[i].vy;
		if (ai[i].x >= whire)
			ai[i].x = whire;
		if (ai[i].y >= high)
			ai[i].y = high;
		if (ai[i].x <= 0)
			ai[i].x = 0;
		if (ai[i].y <= 0)
			ai[i].y = 0;
		solidcircle(ai[i].x, ai[i].y, ai[i].r);
	}
}

double distance(qiu a, qiu b)
{
	return sqrt((double)(a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

void crash(void)//碰撞
{
	for (int i = 0; i < Ai_num; i++)
	{
		float dist = distance(player, ai[i]);
		if (dist< (player.r + ai[i].r) * 0.9 && player.r>ai[i].r)//1 && player.r>ai[i].r * 1.1)//玩家吃ai
		{
			ai[i].flag = 0;
			clearcircle(ai[i].x, ai[i].y, ai[i].r);
			player.r += ai[i].r * 0.5;
		}
		else if (dist < (player.r + ai[i].r * 0.9) && player.r < ai[i].r) //ai吃玩家
		{
			player.flag = 0;
			clearcircle(player.x, player.y, player.r);
			ai[i].r += player.r * 0.5;
		}
	}

	for (int i = 0; i < Food_num; i++)//玩家吃food
	{
		float dist = distance(player, food[i]);
		if (dist < (player.r + food[i].r * 0.5) && player.r > food[i].r)
		{
			food[i].flag = 0;
			clearcircle(food[i].x, food[i].y, food[i].r);
			player.r += food[i].r * 0.5;
		}
	}

	for (int i = 0; i < Ai_num; i++)//ai吃food
	{
		for (int j = 0; j < Food_num; j++)
		{
			float dist = distance(ai[i], food[j]);
			if (dist < (ai[i].r + food[j].r * 0.5) && ai[i].r > food[j].r)
			{
				food[j].flag = 0;
				clearcircle(food[i].x, food[i].y, food[i].r);
				ai[i].r += food[j].r * 0.5;
			}
		}
	}

	for (int i = 0; i < Ai_num; i++)//ai吃ai
	{
		for (int j = 0; j < Ai_num; j++)
		{
			float dist = distance(ai[i], ai[j]);
			if ((dist < (ai[i].r + ai[j].r * 0.5) && ai[i].r > ai[j].r))
			{
				ai[j].flag = 0;
				clearcircle(ai[j].x, ai[j].y, ai[j].r);
				ai[i].r += ai[j].r * 0.5;
			}
			else if (dist < (ai[i].r + ai[j].r * 0.5) && ai[i].r < food[j].r)
			{
				ai[i].flag = 0;
				clearcircle(ai[i].x, ai[i].y, ai[i].r);
				ai[j].r += ai[i].r * 0.5;
			}
		}
	}
}

void draw(void)//绘制
{

	//绘制玩家
	setfillcolor(RED);
	solidcircle(player.x, player.y, player.r);//绘制玩家位置

	setfillcolor(GREEN);
	for(int i=0;i<Ai_num;i++)
		solidcircle(ai[i].x, ai[i].y, ai[i].r);//绘制ai位置

	for (int i = 0; i < Food_num; i++)//绘制food
	{
		if (food[i].flag == 0)
		{
			setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256));
			solidcircle(food[i].x, food[i].y, food[i].r);
			food[i].flag = 1;
		}
		
	}

}

void rebirth(void)//重生
{
	if (player.flag == 0)
	{
		player.x = rand() % whire;
		player.y = rand() % high;
		player.r = 10;
		player.flag = 1;
	}

	for (int i = 0; i < Ai_num; i++)
	{
		if (ai[i].flag == 0)
		{
			ai[i].x = rand() % whire;
			ai[i].y = rand() % high;
			ai[i].r = 10;
			ai[i].flag = 1;
		}
	}

	for (int i = 0; i < Food_num; i++)
	{
		if (food[i].flag == 0)
		{
				food[i].x = rand() % whire;
				food[i].y = rand() % high;

			if (Food_num % 2 != 0)
				food[i].r = 3;
			else
				food[i].r = 2;
		}
	}
}



int main()
{
	init();
	while (1)
	{
		Ai_move();
		player_move();
		draw();
		crash();
		rebirth();
		Sleep(2);
	}
	return 0;
}